package com.acce.gxsc.page.supervisionReport.list;

import android.graphics.Bitmap;
import android.view.View;

import com.acce.gxsc.common.App;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.DutyLogBean;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.DutyBean;
import com.acce.gxsc.model.netBean.NetSupervisionDutyListBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.utils.ToastUtils;
import com.sunmi.impl.V1Printer;
import com.sunmi.util.ThreadPoolManager;

import java.text.MessageFormat;

import rx.Subscriber;

/**
 * Created by huxley on 2017/5/26.
 */

public class SupervisionReportListPresenter extends BasePresenter<SupervisionReportListView> {

    public void loadDataList(String queryDate) {
        WorkModel.getInstance().loadSupervisionReportList(queryDate).subscribe(new Subscriber<ResultBean<NetSupervisionDutyListBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<NetSupervisionDutyListBean> datas) {
                if (mViewRef != null) {
                    ToastUtils.show(datas.returnMsg);
                    getView().loadSupervisionReportListSuccess(datas.result.statList);
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    ToastUtils.show("该班次没有监察过车辆!");
                    getView().hideProgressDialog();
                }
            }
        });
    }



    private V1Printer printer;
    private Bitmap    orderBitmap;

    void printOrder(final View orderView) {
        if (mViewRef != null) {
            if (orderBitmap == null) {
               orderBitmap = Tools.convertViewToBitmap(orderView, 0.85f);
            }else{
                orderBitmap=null;
                orderBitmap = Tools.convertViewToBitmap(orderView, 0.85f);
            }
            orderView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        ThreadPoolManager.getInstance().executeTask(new Runnable() {
                            @Override
                            public void run() {
                                if (printer == null) {
                                    printer = new V1Printer(App.context);
                                }
                                printer.beginTransaction();
                                printer.setAlignment(1);
                                printer.printBitmap(orderBitmap);
                                printer.lineWrap(4);
                                printer.printHorizontalLine(1, 0);
                                printer.lineWrap(2);
                                printer.commitTransaction();
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                if (mViewRef != null) {
                                    orderView.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            getView().hideProgressDialog();
                                        }
                                    }, 1500);
                                }

                            }
                        });
                    } catch (Exception e) {
                        ToastUtils.show("打印失败！");
                    }

                }
            }, 300);
        }

    }
    public void  currentOnTime(final String string){
        WorkModel.getInstance().loadWork().subscribe(new Subscriber<ResultBean<DutyLogBean>>() {
            @Override
            public void onStart(){

            }

            @Override
            public void onNext(ResultBean<DutyLogBean> datas) {
               DutyBean mDutyBean = datas.result.DutyLog;
                switch (mDutyBean.ondutyClazz) {
                    case "1":
                        App.currentOnTime = MessageFormat.format("{0}  早班",string);
                        break;
                    case "2":
                        App.currentOnTime = MessageFormat.format("{0}  中班",string);
                        break;
                    default:
                        App.currentOnTime = MessageFormat.format("{0}  晚班",string);
                        break;
                }

            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                ToastUtils.show("加载失败！");
            }
        });

    }
}
